package de.stefanocke.roo.wicket;

import java.util.List;

import org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue;
import org.springframework.roo.classpath.details.annotations.AnnotationMetadata;
import org.springframework.roo.model.JavaSymbolName;
import org.springframework.roo.model.JavaType;

/**
 * Workaround:  Since DefaultAnnotationMetadata does not implement hashcode, we provide a wrapper
 * here, that uses the string representation of the annotation to provide hash and equals.
 * 
 * @author Administrator
 *
 */
public class AnnotationMetadataWithHashcode implements AnnotationMetadata {

	private AnnotationMetadata origin;

	public AnnotationMetadataWithHashcode(AnnotationMetadata origin) {
		this.origin = origin;
	}

	public AnnotationAttributeValue<?> getAttribute(final JavaSymbolName attributeName) {
		return origin.getAttribute(attributeName);
	}

	@SuppressWarnings("unchecked")
	public AnnotationAttributeValue<?> getAttribute(final String attributeName) {
		return origin.getAttribute(attributeName);
	}

	public List<JavaSymbolName> getAttributeNames() {
		return origin.getAttributeNames();
	}

	public JavaType getAnnotationType() {
		return origin.getAnnotationType();
	}

	@Override
	public String toString() {
		
		StringBuffer sb = new StringBuffer();
		sb.append("annotationType = ");
		sb.append(origin.getAnnotationType());
		sb.append(", attributes = list[");
		List<JavaSymbolName> attributeNames = origin.getAttributeNames();
		for (JavaSymbolName name : attributeNames) {
			AnnotationAttributeValue<?> attribute = origin.getAttribute(name);
			if(attribute!=null){
				sb.append(attribute.toString());
				sb.append(" ");
			}
		}
		sb.append("]");
		return sb.toString();
	}

	@Override
	public int hashCode() {
		return toString().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		
		return toString().equals(obj.toString());
	}
	
	

}
